{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ch09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 理解什么是类，为什么会有类\n",
    "\n",
    "\n",
    "# 9.1 创造和使用类\n",
    "\n",
    "class Dog():  ### 首字母大写，不能使用下划线\n",
    "    \"\"\"一次模拟小狗的简单测试\"\"\"\n",
    "\n",
    "    def __init__(self,name,age): # init自动变色，self必须有\n",
    "        \"\"\"初始化属性name和age\"\"\"\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "\n",
    "    def sit(self):\n",
    "        \"\"\"模拟小狗被命令蹲下\"\"\"\n",
    "        print(self.name.title()+\" is now sitting.\")\n",
    "\n",
    "    def roll_over(self):\n",
    "        \"\"\"模拟小狗被命令时打滚\"\"\"\n",
    "        print(self.name.title()+' rolled over!')\n",
    "\n",
    "my_dog= Dog('willie', 6)\n",
    "a=my_dog.name\n",
    "print(a)\n",
    "my_dog.sit()\n",
    "my_dog.roll_over()\n",
    "\n",
    "print(\"My dog's name is \"+my_dog.name.title()+'.')\n",
    "print(\"My dog is \"+str(my_dog.age)+\" years old.\")\n",
    "\n",
    "your_dog=Dog('lucy',3)\n",
    "print(\"\\nYour dog's name is \"+your_dog.name.title()+'.')\n",
    "print(\"Your dog is \"+str(your_dog.age)+\" years old.\")\n",
    "your_dog.sit()\n",
    "\n",
    "\n",
    "\n",
    "# 9.2 汽车类\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "my_new_car=Car('audi','a4','2016')\n",
    "print(my_new_car.get_descriptive_name())\n",
    "\n",
    "#加入带默认值得属性，直接修改，防止回调\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "        self.odometer_reading=0\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "    def read_odometer(self):\n",
    "        print(\"This car has \"+str(self.odometer_reading)+' miles on it.')\n",
    "\n",
    "    def update_odometer(self,mileage):\n",
    "        \"\"\"打印一条指出汽车里程的消息\"\"\"\n",
    "        if mileage>=self.odometer_reading:\n",
    "            self.odometer_reading=mileage\n",
    "        else:\n",
    "            print(\"You cannot roll back an odometer\")\n",
    "\n",
    "\n",
    "    def increment_odometer(self,miles):\n",
    "        \"\"\"将里程表读数增加指定的量\"\"\"\n",
    "        self.odometer_reading+=miles\n",
    "\n",
    "\n",
    "\n",
    "my_new_car=Car('audi','a4','2016')\n",
    "print(my_new_car.get_descriptive_name())\n",
    "\n",
    "\n",
    "my_used_car=Car('subaru','outback',2013)\n",
    "print(my_used_car.get_descriptive_name())\n",
    "\n",
    "my_used_car.update_odometer(23500)\n",
    "my_used_car.read_odometer()\n",
    "\n",
    "my_used_car.increment_odometer(100)\n",
    "my_used_car.read_odometer()\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 9.3 继承\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "        self.odometer_reading=0\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "    def read_odometer(self):\n",
    "        print(\"This car has \"+str(self.odometer_reading)+' miles on it.')\n",
    "\n",
    "    def update_odometer(self,mileage):\n",
    "        \"\"\"打印一条指出汽车里程的消息\"\"\"\n",
    "        if mileage>=self.odometer_reading:\n",
    "            self.odometer_reading=mileage\n",
    "        else:\n",
    "            print(\"You cannot roll back an odometer\")\n",
    "\n",
    "\n",
    "    def increment_odometer(self,miles):\n",
    "        \"\"\"将里程表读数增加指定的量\"\"\"\n",
    "        self.odometer_reading+=miles\n",
    "\n",
    "    def fill_gas_tank(self, gas):\n",
    "        \"\"\"汽车油箱还有多少油\"\"\"\n",
    "        print('油箱还有：' + str(gas) + 'ml')\n",
    "\n",
    "\n",
    "\n",
    "class ElectricCar(Car): # Car在前面，写在括号里\n",
    "    \"\"\"电动汽车的独特之处\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化父类的属性\"\"\"\n",
    "        super().__init__(make,model,year)\n",
    "        #super()是一个特殊的函数，调用父类属性\n",
    "        self.battery_size=70\n",
    "\n",
    "    def describe_battery(self):\n",
    "        \"\"\"打印一条描述电瓶容量的信息\"\"\"\n",
    "        print(\"This car has a \"+str(self.battery_size)+'-kWh battery.')\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.describe_battery()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9.2 汽车类\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "my_new_car=Car('audi','a4','2016')\n",
    "print(my_new_car.get_descriptive_name())\n",
    "\n",
    "#加入带默认值得属性，直接修改，防止回调\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "        self.odometer_reading=0\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "    def read_odometer(self):\n",
    "        print(\"This car has \"+str(self.odometer_reading)+' miles on it.')\n",
    "\n",
    "    def update_odometer(self,mileage):\n",
    "        \"\"\"打印一条指出汽车里程的消息\"\"\"\n",
    "        if mileage>=self.odometer_reading:\n",
    "            self.odometer_reading=mileage\n",
    "        else:\n",
    "            print(\"You cannot roll back an odometer\")\n",
    "\n",
    "\n",
    "    def increment_odometer(self,miles):\n",
    "        \"\"\"将里程表读数增加指定的量\"\"\"\n",
    "        self.odometer_reading+=miles\n",
    "\n",
    "\n",
    "\n",
    "my_new_car=Car('audi','a4','2016')\n",
    "print(my_new_car.get_descriptive_name())\n",
    "\n",
    "\n",
    "my_used_car=Car('subaru','outback',2013)\n",
    "print(my_used_car.get_descriptive_name())\n",
    "\n",
    "my_used_car.update_odometer(23500)\n",
    "my_used_car.read_odometer()\n",
    "\n",
    "my_used_car.increment_odometer(100)\n",
    "my_used_car.read_odometer()\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9.3 继承\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "        self.odometer_reading=0\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "    def read_odometer(self):\n",
    "        print(\"This car has \"+str(self.odometer_reading)+' miles on it.')\n",
    "\n",
    "    def update_odometer(self,mileage):\n",
    "        \"\"\"打印一条指出汽车里程的消息\"\"\"\n",
    "        if mileage>=self.odometer_reading:\n",
    "            self.odometer_reading=mileage\n",
    "        else:\n",
    "            print(\"You cannot roll back an odometer\")\n",
    "\n",
    "\n",
    "    def increment_odometer(self,miles):\n",
    "        \"\"\"将里程表读数增加指定的量\"\"\"\n",
    "        self.odometer_reading+=miles\n",
    "\n",
    "    def fill_gas_tank(self, gas):\n",
    "        \"\"\"汽车油箱还有多少油\"\"\"\n",
    "        print('油箱还有：' + str(gas) + 'ml')\n",
    "\n",
    "\n",
    "\n",
    "class ElectricCar(Car): # Car在前面，写在括号里\n",
    "    \"\"\"电动汽车的独特之处\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化父类的属性\"\"\"\n",
    "        super().__init__(make,model,year)\n",
    "        #super()是一个特殊的函数，调用父类属性\n",
    "        self.battery_size=70\n",
    "\n",
    "    def describe_battery(self):\n",
    "        \"\"\"打印一条描述电瓶容量的信息\"\"\"\n",
    "        print(\"This car has a \"+str(self.battery_size)+'-kWh battery.')\n",
    "\n",
    "    def fill_gas_tank(self):\n",
    "        \"\"\"电动汽车没有油箱\"\"\"\n",
    "        print('俺们电动汽车没有油箱')\n",
    "\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.describe_battery()\n",
    "\n",
    "# 9.3.4 重写父类 直接加定义函数\n",
    "my_tesla.fill_gas_tank()\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例作为属性，在类函数里面引用一个类\n",
    "class Car():\n",
    "    \"\"\"一次模拟汽车的简单尝试\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化汽车描述的属性\"\"\"\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "        self.odometer_reading=0\n",
    "\n",
    "    def get_descriptive_name(self):\n",
    "        \"\"\"返回整洁的描述性信息\"\"\"\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return  long_name.title()\n",
    "\n",
    "    def read_odometer(self):\n",
    "        print(\"This car has \"+str(self.odometer_reading)+' miles on it.')\n",
    "\n",
    "    def update_odometer(self,mileage):\n",
    "        \"\"\"打印一条指出汽车里程的消息\"\"\"\n",
    "        if mileage>=self.odometer_reading:\n",
    "            self.odometer_reading=mileage\n",
    "        else:\n",
    "            print(\"You cannot roll back an odometer\")\n",
    "\n",
    "\n",
    "    def increment_odometer(self,miles):\n",
    "        \"\"\"将里程表读数增加指定的量\"\"\"\n",
    "        self.odometer_reading+=miles\n",
    "\n",
    "\n",
    "\n",
    "class Battery():\n",
    "    \"\"\"一次模拟电动汽车电瓶的简化尝试\"\"\"\n",
    "\n",
    "    def __init__(self,battery_size=70):\n",
    "        \"\"\"初始化电瓶的属性\"\"\"\n",
    "        self.battery_size=battery_size\n",
    "\n",
    "    def describe_battery(self):\n",
    "        \"\"\"打印一条描述电瓶容量的消息\"\"\"\n",
    "        print(\"This car has a \"+str(self.battery_size)+\"-kwh battery.\")\n",
    "\n",
    "    def get_range(self):\n",
    "        \"\"\"打印一条消息，指出电瓶的续航里程\"\"\"\n",
    "        if self.battery_size==70:\n",
    "            range=240\n",
    "        elif self.battery_size==85:\n",
    "            range=270\n",
    "\n",
    "        message=\"This car can go approximately \"+str(range)\n",
    "        message+=\" miles on a full charge.\"\n",
    "        print(message)\n",
    "\n",
    "class ElectricCar(Car):\n",
    "    \"\"\"电动汽车的独特之处\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化父类的属性\"\"\"\n",
    "        super().__init__(make,model,year)\n",
    "\n",
    "\n",
    "        self.battery=Battery()\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9.4 导入类\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "from car import Car\n",
    "\n",
    "class Battery():\n",
    "    \"\"\"一次模拟电动汽车电瓶的简化尝试\"\"\"\n",
    "\n",
    "    def __init__(self,battery_size=70):\n",
    "        \"\"\"初始化电瓶的属性\"\"\"\n",
    "        self.battery_size=battery_size\n",
    "\n",
    "    def describe_battery(self):\n",
    "        \"\"\"打印一条描述电瓶容量的消息\"\"\"\n",
    "        print(\"This car has a \"+str(self.battery_size)+\"-kwh battery.\")\n",
    "\n",
    "    def get_range(self):\n",
    "        \"\"\"打印一条消息，指出电瓶的续航里程\"\"\"\n",
    "        if self.battery_size==70:\n",
    "            range=240\n",
    "        elif self.battery_size==85:\n",
    "            range=270\n",
    "\n",
    "\n",
    "\n",
    "        message=\"This car can go approximately \"+str(range)\n",
    "        message+=\" miles on a full charge.\"\n",
    "        print(message)\n",
    "\n",
    "class ElectricCar(Car):\n",
    "    \"\"\"电动汽车的独特之处\"\"\"\n",
    "\n",
    "    def __init__(self,make,model,year):\n",
    "        \"\"\"初始化父类的属性\"\"\"\n",
    "        super().__init__(make,model,year)\n",
    "\n",
    "\n",
    "        self.battery=Battery()\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()\n",
    "\n",
    "\n",
    "# 9.4.3 从一个模块导入多个类\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "from car_electriccar import Car,ElectricCar\n",
    "\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 9.4.4 或者直接导入模块，用.去引用具体类，推荐使用这种方法\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "import car_electriccar\n",
    "\n",
    "\n",
    "my_tesla=car_electriccar.ElectricCar('tesla','model s',2016)\n",
    "\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()\n",
    "\n",
    "# 9.4.5 导入所有类，不推荐，能看懂就行了,类名称容易冲突\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "from car_electriccar import *\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()\n",
    "\n",
    "\n",
    "# 9.4.6 一个模块引入\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "from car import Car\n",
    "from electriccar import ElectricCar\n",
    "\n",
    "my_used_car=Car('subaru','outback',2013)\n",
    "print(my_used_car.get_descriptive_name())\n",
    "\n",
    "my_tesla=ElectricCar('tesla','model s',2016)\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()\n",
    "\n",
    "# 9.5 Python标准库，从模块collection中导类\n",
    "\n",
    "from collections import OrderedDict\n",
    "\n",
    "favorite_languages=OrderedDict()\n",
    "##创建一个空的有序字典\n",
    "\n",
    "favorite_languages['jen']='python'\n",
    "favorite_languages['sarah']='c'\n",
    "favorite_languages['edward']='ruby'\n",
    "favorite_languages['phil']='python'\n",
    "\n",
    "for name, language in favorite_languages.items():\n",
    "    print(name.title()+\"'s favorite language is \"+language.title()+'.')\n",
    "\n",
    "# 9.6 字典编码风格 大写不能有下划线 文档字符串\n",
    "\n",
    "# class Dog\n",
    "\n",
    "# def_1\n",
    "\n",
    "# def_2\n",
    "\n",
    "# Class_1\n",
    "\n",
    "\n",
    "# Class_2\n",
    "\n",
    "## import 导入，先导入标准库\n",
    "\n",
    "## import 自定义模块中的类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9-1\n",
    "\n",
    "class Restaurant(): ##啊啊大小写啊，class小写，R大写\n",
    "    \"\"\"模拟饭店调查\"\"\"\n",
    "\n",
    "    def __init__(self,restaurant_name,cuisine_type):\n",
    "        # 不要忘记def空格__两个下划线\n",
    "        self.name=restaurant_name\n",
    "        self.type=cuisine_type\n",
    "\n",
    "    def describe_restaurant(self): #不要忘记这个self\n",
    "        print(self.name+'的'+self.type+'很不错')\n",
    "\n",
    "    def open_restaurant(self):\n",
    "        print('而且他正在营业')\n",
    "        ## 内部参数，不要忘了self\n",
    "\n",
    "restaurant=Restaurant('杨国福','麻辣烫')\n",
    "restaurant.describe_restaurant()\n",
    "restaurant.open_restaurant()\n",
    "\n",
    "# 9-2\n",
    "\n",
    "restaurant_1=Restaurant('马景涛','麻辣烫')\n",
    "restaurant_1.describe_restaurant()\n",
    "\n",
    "restaurant_2=Restaurant('新玛特','麻辣烫')\n",
    "restaurant_2.describe_restaurant()\n",
    "\n",
    "restaurant_3=Restaurant('国泰','麻辣烫')\n",
    "restaurant_3.describe_restaurant()\n",
    "\n",
    "# 9-3\n",
    "\n",
    "class User():\n",
    "    \"\"\"记录用户信息\"\"\"\n",
    "\n",
    "    def __init__(self,xing,ming,*qita):\n",
    "        self.xing=xing\n",
    "        self.ming=ming\n",
    "        self.qita=qita\n",
    "\n",
    "    def describe_user(self):\n",
    "        print(\"姓：\"+self.xing)\n",
    "        print(\"名：\"+self.ming)\n",
    "        if self.qita:\n",
    "            print('\\n其他：\\n')\n",
    "            for info in self.qita:\n",
    "                print(\"-\"+info)\n",
    "\n",
    "    def greet_user(self):\n",
    "        print('\\n'+self.ming + '仔,我们奈你！')\n",
    "        print(self.ming+'仔,你老霸道了！')\n",
    "\n",
    "zjds=User('致敬大',\n",
    "          '神',\n",
    "          '美','帅呆','机智','成功')\n",
    "\n",
    "zjds.describe_user()\n",
    "zjds.greet_user()\n",
    "\n",
    "# 9-4 加入就餐人数\n",
    "\n",
    "class Restaurant():\n",
    "    \"\"\"模拟饭店调查\"\"\"\n",
    "\n",
    "    def __init__(self,restaurant_name,cuisine_type):\n",
    "        # 不要忘记def空格__两个下划线\n",
    "        self.name=restaurant_name\n",
    "        self.type=cuisine_type\n",
    "        self.number_served=0\n",
    "\n",
    "    def describe_restaurant(self): #不要忘记这个self\n",
    "        print(self.name+'的'+self.type+'很不错')\n",
    "\n",
    "    def open_restaurant(self):\n",
    "        print('而且他正在营业')\n",
    "        ## 内部参数，不要忘了self\n",
    "\n",
    "    def set_number_served(self,number):\n",
    "\n",
    "        if number>=self.number_served:\n",
    "            self.number_served=number\n",
    "\n",
    "        print('共有'+str(self.number_served)+'人在此用餐过。')\n",
    "\n",
    "    def increment_number_served(self,increment_number):\n",
    "        self.number_served+=increment_number\n",
    "        print('共有' + str(self.number_served) + '人在此用餐过。')\n",
    "\n",
    "\n",
    "restaurant=Restaurant('杨国福','麻辣烫')\n",
    "restaurant.set_number_served(100)\n",
    "restaurant.increment_number_served(50)\n",
    "\n",
    "# 9-5 尝试登陆次数\n",
    "\n",
    "class User():\n",
    "    \"\"\"记录用户信息\"\"\"\n",
    "\n",
    "    def __init__(self,xing,ming,*qita):\n",
    "        self.xing=xing\n",
    "        self.ming=ming\n",
    "        self.qita=qita\n",
    "        self.login_attempts=0\n",
    "\n",
    "    def describe_user(self):\n",
    "        print(\"姓：\"+self.xing)\n",
    "        print(\"名：\"+self.ming)\n",
    "        if self.qita:\n",
    "            print('\\n其他：\\n')\n",
    "            for info in self.qita:\n",
    "                print(\"-\"+info)\n",
    "\n",
    "    def greet_user(self):\n",
    "        print('\\n'+self.ming + '仔,我们奈你！')\n",
    "        print(self.ming+'仔,你老霸道了！')\n",
    "\n",
    "    def increment_login_attempts(self):\n",
    "        self.login_attempts+=1\n",
    "\n",
    "    def reset_login_attempts(self):\n",
    "        self.login_attempts=0\n",
    "\n",
    "    def print_login_attempts(self):\n",
    "        print('您登陆了'+str(self.login_attempts)+'次')\n",
    "\n",
    "\n",
    "zjds=User('致敬大',\n",
    "          '神',\n",
    "          '美','帅呆','机智','成功')\n",
    "\n",
    "zjds.increment_login_attempts()\n",
    "zjds.increment_login_attempts()\n",
    "zjds.increment_login_attempts()\n",
    "zjds.print_login_attempts()\n",
    "zjds.reset_login_attempts()\n",
    "zjds.print_login_attempts()\n",
    "\n",
    "\n",
    "\n",
    "# 9-6 继承Restaurant\n",
    "\n",
    "class Restaurant():\n",
    "    \"\"\"模拟饭店调查\"\"\"\n",
    "\n",
    "    def __init__(self,restaurant_name,cuisine_type):\n",
    "        # 不要忘记def空格__两个下划线\n",
    "        self.name=restaurant_name\n",
    "        self.type=cuisine_type\n",
    "        self.number_served=0\n",
    "\n",
    "    def describe_restaurant(self): #不要忘记这个self\n",
    "        print(self.name+'的'+self.type+'很不错')\n",
    "\n",
    "    def open_restaurant(self):\n",
    "        print('而且他正在营业')\n",
    "        ## 内部参数，不要忘了self\n",
    "\n",
    "    def set_number_served(self,number):\n",
    "\n",
    "        if number>=self.number_served:\n",
    "            self.number_served=number\n",
    "\n",
    "        print('共有'+str(self.number_served)+'人在此用餐过。')\n",
    "\n",
    "    def increment_number_served(self,increment_number):\n",
    "        self.number_served=increment_number\n",
    "        print('共有' + str(self.number_served) + '人在此用餐过。')\n",
    "\n",
    "class IceCreamStand(Restaurant):\n",
    "    \"\"\"继承9-4，冰淇淋特殊餐馆\"\"\"\n",
    "\n",
    "    def __init__(self,restaurant_name,cuisine_type):\n",
    "\n",
    "        super().__init__(restaurant_name,cuisine_type)\n",
    "        self.flavors=['vanilla','chocolate','durian',]\n",
    "\n",
    "    def print_flavors(self):\n",
    "        print(\"\\n\")\n",
    "        for flavor in self.flavors:\n",
    "            print(flavor)\n",
    "\n",
    "restaurant_4=IceCreamStand('麦当劳甜品站','冰淇淋')\n",
    "restaurant_4.print_flavors()\n",
    "\n",
    "# 9-7 继承User，管理员\n",
    "\n",
    "class User():\n",
    "    \"\"\"记录用户信息\"\"\"\n",
    "\n",
    "    def __init__(self,xing,ming,*qita):\n",
    "        self.xing=xing\n",
    "        self.ming=ming\n",
    "        self.qita=qita\n",
    "\n",
    "    def describe_user(self):\n",
    "        print(\"姓：\"+self.xing)\n",
    "        print(\"名：\"+self.ming)\n",
    "        if self.qita:\n",
    "            print('\\n其他：\\n')\n",
    "            for info in self.qita:\n",
    "                print(\"-\"+info)\n",
    "\n",
    "    def greet_user(self):\n",
    "        print('\\n'+self.ming + '仔,我们奈你！')\n",
    "        print(self.ming+'仔,你老霸道了！')\n",
    "\n",
    "class Admin(User):\n",
    "    \"\"\"特殊的用户，管理员\"\"\"\n",
    "\n",
    "    def __init__(self,xing,ming,*qita):\n",
    "\n",
    "        super().__init__(xing,ming,*qita)\n",
    "        self.privileges='和稀泥'\n",
    "\n",
    "    def show_privileges(self):\n",
    "        print('\\n管理员能'+self.privileges)\n",
    "\n",
    "\n",
    "zjds=Admin('致敬大',\n",
    "          '神',\n",
    "          '美','帅呆','机智','成功')\n",
    "\n",
    "zjds.show_privileges()\n",
    "\n",
    "# 9-8 权限\n",
    "\n",
    "\n",
    "\n",
    "class User():\n",
    "    \"\"\"记录用户信息\"\"\"\n",
    "\n",
    "    def __init__(self,xing,ming,*qita):\n",
    "        self.xing=xing\n",
    "        self.ming=ming\n",
    "        self.qita=qita\n",
    "\n",
    "    def describe_user(self):\n",
    "        print(\"姓：\"+self.xing)\n",
    "        print(\"名：\"+self.ming)\n",
    "        if self.qita:\n",
    "            print('\\n其他：\\n')\n",
    "            for info in self.qita:\n",
    "                print(\"-\"+info)\n",
    "\n",
    "    def greet_user(self):\n",
    "        print('\\n'+self.ming + '仔,我们奈你！')\n",
    "        print(self.ming+'仔,你老霸道了！')\n",
    "\n",
    "class Admin(User):\n",
    "    \"\"\"特殊的用户，管理员\"\"\"\n",
    "\n",
    "    def __init__(self,xing,ming,*qita):\n",
    "\n",
    "        super().__init__(xing,ming,*qita)\n",
    "\n",
    "\n",
    "    def show_privileges(self):\n",
    "        Privileges()\n",
    "\n",
    "class Privileges():\n",
    "    \"\"\"管理人员特权\"\"\"\n",
    "\n",
    "    def __init__(self):\n",
    "        self.privileges = '和稀泥'\n",
    "        print('\\n管理员能' + self.privileges)\n",
    "\n",
    "\n",
    "zjds=Admin('致敬大',\n",
    "          '神',\n",
    "          '美','帅呆','机智','成功')\n",
    "\n",
    "zjds.show_privileges()\n",
    "\n",
    "\n",
    "# 9-9 电瓶升级\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "import car_electriccar\n",
    "\n",
    "new_car=car_electriccar.ElectricCar('aima','A4','2013')\n",
    "new_car.battery.upgrade_battery()\n",
    "new_car.battery.get_range()\n",
    "\n",
    "# 9-10 导入Restaurant\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "import restaurant\n",
    "\n",
    "r=restaurant.Restaurant('杨国福','麻辣烫')\n",
    "r.describe_restaurant()\n",
    "r.open_restaurant()\n",
    "\n",
    "# 9-11 导入Admin，调用9-8的程序\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "import admin\n",
    "\n",
    "zjds=admin.Admin('致敬大','神','美','帅呆','机智','成功')\n",
    "\n",
    "zjds.show_privileges()\n",
    "\n",
    "# 9-12 导入多个模块\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "import admin_import_user\n",
    "\n",
    "zjds=admin_import_user.Admin('致敬大','神','美','帅呆','机智','成功')\n",
    "\n",
    "zjds.show_privileges()\n",
    "\n",
    "# 9-13 OrderDict,重写6-4\n",
    "## 习题6-3,6-4粘贴过来\n",
    "python_words={'Code lay out':'代码布局',\n",
    "              'Whitespaces in Expressions':'表达式中的空格',\n",
    "              'Comments':'注释',\n",
    "              'Naming Conventions':'命名规范',\n",
    "              'programming recommendations':'编程建议',\n",
    "              'Global Variable Names':'全局变量名',\n",
    "              'Function Annotation':'功能注释',\n",
    "              'Indentation':'缩进',\n",
    "              'Binary Operator':'运算符',\n",
    "              'String quotes':'字符串引号'\n",
    "              }\n",
    "\n",
    "for word, explaination in python_words.items():\n",
    "    print(\"\\n\"+word+\":\")\n",
    "    print(explaination)\n",
    "\n",
    "## 改写\n",
    "from collections import OrderedDict\n",
    "\n",
    "python_words = OrderedDict()\n",
    "\n",
    "python_words['Code lay out']='代码布局'\n",
    "python_words['Whitespaces in Expressions']='表达式中的空格'\n",
    "python_words['Comments']='注释'\n",
    "python_words['Naming Conventions']='命名规范'\n",
    "python_words['String quotes']='字符串引号'\n",
    "\n",
    "n=0\n",
    "for word, explaination in python_words.items():\n",
    "    print(\"\\n\"+str(n)+' '+word+\":\")\n",
    "    print(explaination)\n",
    "    n+=1\n",
    "\n",
    "# 9-14 随机数模拟骰子\n",
    "\n",
    "from random import randint\n",
    "\n",
    "##测试一下这个函数的功能\n",
    "x = randint(1,6) #闭区间\n",
    "print(x)\n",
    "x = randint(1,6)\n",
    "print(x)\n",
    "x = randint(1,6)\n",
    "print(x)\n",
    "\n",
    "from random import randint\n",
    "x = randint(1,6)\n",
    "print(x)\n",
    "print(x)\n",
    "print(x)\n",
    "\n",
    "## 定义一个类，x个面的筛子掷n次\n",
    "\n",
    "from random import randint\n",
    "class Die():\n",
    "\n",
    "    def __init__(self,sides):\n",
    "        self.sides=sides\n",
    "\n",
    "    def roll_die(self):\n",
    "        self.side=randint(1,self.sides)\n",
    "        print(self.side)\n",
    "\n",
    "    def roll_die_times(self,times):\n",
    "\n",
    "        self.n=0\n",
    "        self.times=times\n",
    "        while self.n<self.times:\n",
    "            self.roll_die()\n",
    "            self.n+=1\n",
    "\n",
    "x=Die(6)\n",
    "x.roll_die_times(10)\n",
    "\n",
    "y=Die(10)\n",
    "y.roll_die_times(5)\n",
    "\n",
    "z=Die(20)\n",
    "z.roll_die_times(10)\n",
    "\n",
    "#9-15 Python Module of the Week https://pymotw.com/3/"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
